package java44th.group.service.admin.category;

import java44th.group.bean.admin.bo.cotegory.DeleteBo;
import java44th.group.bean.admin.bo.cotegory.UpdateBo;
import java44th.group.bean.po.MarketCategory;
import java44th.group.bean.po.MarketCategoryExample;
import java44th.group.bean.admin.vo.category.CreateVo;
import java44th.group.bean.admin.vo.category.L1TypeVo;
import java44th.group.bean.admin.vo.category.ListVo;
import java44th.group.mapper.MarketCategoryMapper;
import java44th.group.utils.LocalHost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Aurhor: 11437
 * @DATE: 2022/10/12 16:24
 * @Description:商品类别
 * @Version 1.0
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper categoryMapper;

    //查询商品类别
    @Override
    public ListVo categoryList() {

        //    商品类别个数
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L1");
        int count = (int) categoryMapper.countByExample(marketCategoryExample);


        ListVo listVo = new ListVo(count, 1, count, 1);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);


        List<ListVo.ListDTO> listDTOS = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {

            if (!marketCategory.getIconUrl().startsWith("http")) {
                marketCategory.setIconUrl(LocalHost.localhost + marketCategory.getIconUrl());
            }
            if (!marketCategory.getPicUrl().startsWith("http")) {
                marketCategory.setPicUrl((LocalHost.localhost + marketCategory.getPicUrl()));

            }

            ListVo.ListDTO listDTO = new ListVo.ListDTO(marketCategory.getId(), marketCategory.getName(), marketCategory.getKeywords(),
                    marketCategory.getDesc(), marketCategory.getIconUrl(), marketCategory.getPicUrl(), marketCategory.getLevel());

            List<ListVo.ListDTO.ChildrenDTO> childrenDTOS = new ArrayList<>();

            MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
            marketCategoryExample1.createCriteria().andPidEqualTo(listDTO.getId());
            List<MarketCategory> marketCategories1 = categoryMapper.selectByExample(marketCategoryExample1);

            for (MarketCategory category : marketCategories1) {

                if (!category.getIconUrl().startsWith("http")) {
                    category.setIconUrl(LocalHost.localhost + category.getIconUrl());
                }
                if (!category.getPicUrl().startsWith("http")) {
                    category.setPicUrl((LocalHost.localhost + category.getPicUrl()));

                }


                ListVo.ListDTO.ChildrenDTO childrenDTO = new ListVo.ListDTO.ChildrenDTO(category.getId(), category.getName(), category.getKeywords(),
                        category.getDesc(), category.getIconUrl(), category.getPicUrl(), category.getLevel());
                childrenDTOS.add(childrenDTO);

            }
            listDTO.setChildren(childrenDTOS);
            listDTOS.add(listDTO);
        }

        listVo.setList(listDTOS);

        return listVo;
    }

    //新建商品类别
    @Override
    public CreateVo createCategory(MarketCategory marketCategory) {
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        if (marketCategory.getPicUrl() != null) {
            marketCategory.setPicUrl(marketCategory.getPicUrl().replace(LocalHost.localhost, ""));
        }
        if (marketCategory.getIconUrl() != null) {

            marketCategory.setIconUrl(marketCategory.getIconUrl().replace(LocalHost.localhost, ""));
        }

        categoryMapper.insertSelective(marketCategory);
        if (marketCategory.getIconUrl() != null) {
            marketCategory.setIconUrl(LocalHost.localhost + marketCategory.getIconUrl());
        }
        if (marketCategory.getPicUrl() != null) {
            marketCategory.setPicUrl(LocalHost.localhost + marketCategory.getPicUrl());
        }
        CreateVo createVo = new CreateVo(marketCategory.getId(), marketCategory.getName(),
                marketCategory.getKeywords(), marketCategory.getDesc(), marketCategory.getPid(), marketCategory.getIconUrl(),
                marketCategory.getPicUrl(), marketCategory.getLevel(), marketCategory.getAddTime().toString(), marketCategory.getUpdateTime().toString()
        );

        return createVo;

    }

    //查询商品类别l1
    @Override
    public L1TypeVo selectTypeL1() {

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L1");
        int count = (int) categoryMapper.countByExample(marketCategoryExample);

        L1TypeVo l1TypeVo = new L1TypeVo();
        l1TypeVo.setLimit(count);
        l1TypeVo.setPage(1);
        l1TypeVo.setPages(1);
        l1TypeVo.setTotal(count);

        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        List<L1TypeVo.ListDTO> listDTOS = new ArrayList();
        for (MarketCategory marketCategory : marketCategories) {
            L1TypeVo.ListDTO listDTO = new L1TypeVo.ListDTO();
            listDTO.setLabel(marketCategory.getName());
            listDTO.setValue(marketCategory.getId());
            listDTOS.add(listDTO);
        }
        l1TypeVo.setList(listDTOS);

        return l1TypeVo;

    }

    //修改商品类别
    @Override
    public void updateCategory(UpdateBo updateBo) {

        if (updateBo.getPicUrl() != null) {
            updateBo.setPicUrl(updateBo.getPicUrl().replace(LocalHost.localhost, ""));
        }
        if (updateBo.getIconUrl() != null) {

            updateBo.setIconUrl(updateBo.getIconUrl().replace(LocalHost.localhost, ""));
        }
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(updateBo.getId());
        marketCategory.setName(updateBo.getName());
        marketCategory.setKeywords(updateBo.getKeywords());
        marketCategory.setDesc(updateBo.getDesc());
        marketCategory.setPid(updateBo.getPid());
        marketCategory.setIconUrl(updateBo.getIconUrl());
        marketCategory.setPicUrl(updateBo.getPicUrl());
        marketCategory.setLevel(updateBo.getLevel());
        marketCategory.setUpdateTime(new Date());
        categoryMapper.updateByPrimaryKeySelective(marketCategory);


        if (updateBo.getChildren() != null) {
            List<UpdateBo.ChildrenDTO> children = updateBo.getChildren();
            for (UpdateBo.ChildrenDTO child : children) {
                MarketCategory marketCategory1 = new MarketCategory();
                marketCategory1.setId(child.getId());
                marketCategory1.setPid(updateBo.getPid());
                categoryMapper.updateByPrimaryKeySelective(marketCategory1);
            }
        }

    }

    //删除商品类别
    @Override
    public void deleteCategory(DeleteBo deleteBo) {


        categoryMapper.deleteByPrimaryKey(deleteBo.getId());


        if (deleteBo.getChildren() != null) {
            List<DeleteBo.ChildrenDTO> children = deleteBo.getChildren();
            for (DeleteBo.ChildrenDTO child : children) {

                categoryMapper.deleteByPrimaryKey(child.getId());
            }


        }
    }
}